Git 專案中徹底刪除某類型副檔名所有檔案

「Python uv 開發環境」通常是指使用 uv(Astral 推出的 Python 套件管理工具,類似 pip/poetry,但更快)來建立隔離環境。這會影響到工具安裝方式,但 Git 本身的命令並不依賴 Python,所以核心步驟不變。差異主要在於 如何安裝 git-filter-repo 或其他輔助工具。


🔹 在 uv 環境下的修改步驟

1. 建立 uv 專案環境

uv init myproject
cd myproject

2. 安裝 git-filter-repo

在 uv 環境中安裝:

uv add git-filter-repo

這樣會把 git-filter-repo 安裝到 uv 管理的虛擬環境裡。

3. 執行刪除 .m4a 的命令

進入 uv shell:

uv run git filter-repo --invert-paths --path-glob '*.m4a'

這裡的差異是:

  • 使用 uv run 來呼叫安裝在 uv 環境裡的工具。
  • 其他 Git 指令(如 git rmgit commitgit push)仍然直接用系統的 Git,不需要透過 uv。
    方法 B:直接強制執行(快速但有風險)
    如果你確定要在現有 repo 上直接操作,可以加上 :
git filter-repo --force --invert-paths --path-glob '*.m4a'

⚠️ 注意:這會直接重寫目前 repo 的歷史,可能導致 repo 內部 packfile 與 reflog 被破壞。建議先備份或至少在另一個 clone 測試。


🔹 完整流程表(uv 環境版)

階段 傳統 Git uv 環境下
移除目前版本檔案 git rm --cached -r '*.m4a'
git commit -m "Remove .m4a"
相同
安裝工具 pip install git-filter-repo uv add git-filter-repo
重寫歷史 git filter-repo --invert-paths --path-glob '*.m4a' uv run git filter-repo --invert-paths --path-glob '*.m4a'
推送乾淨版本 git push origin --force --all 相同

⚠️ 注意

  • uv 只影響 Python 套件的安裝與執行方式,Git 本身仍是系統層工具。
  • 如果團隊成員沒有 uv,他們只要有 Git 就能使用清理後的 repo。
  • 建議在 .gitignore 裡加上 *.m4a,避免未來再被加入。

要不要我幫你把這個流程再整理成一個 「快速命令清單」,讓你在 uv 環境裡可以直接複製貼上執行?

Also See

File2FileLast Modified
在 Git 資料庫中列出所有 .m4a檔案,並計算它們在 目前版本和整個歷史紀錄中所佔的位元組數在 Git 資料庫中列出所有 .m4a檔案,並計算它們在 目前版本和整個歷史紀錄中所佔的位元組數2:24 PM - January 12, 2026
git內部完全刪除一個檔案git內部完全刪除一個檔案2:24 PM - January 12, 2026